package rpg.render
{
	import flash.display.Sprite;
	
	import rpg.Utils.Val;
	import rpg.manager.GameManager;
	import rpg.update.Update;


	public class RenderManager extends Update
	{
		protected var container : Sprite;
		protected var renderList : Array = [];
		private static var instance : RenderManager;

		public function RenderManager()
		{
			super();
			container = GameManager.getInstance().getContainer()
			registerd(Val.PRIORITY_HIGH);
			this.frameRate = 8;

		}

		public static function getInstance() : RenderManager
		{
			if(instance == null)
			{
				instance = new RenderManager();
			}
			return instance;
		}

		public function get length() : int
		{
			return renderList.length;
		}

		/**
		 * 添加渲染对象
		 * @param render
		 * @return
		 *
		 */
		public function addRender(render : RenderData) : Boolean
		{
			if(render == null)
				return false;

			if(renderList.indexOf(render) == -1)
			{
				var index : int = renderList.push(render);
				addToContainer(render, index);
				return true;
			}
			return false;
		}

		/**
		 * 移除渲染对象
		 * @param render
		 * @return
		 *
		 */
		public function removeRender(render : RenderData) : Boolean
		{
			if(render == null)
				return false;
			var index : int = renderList.indexOf(render);
			removeFromContainer(render);

			if(index != -1)
			{
				renderList.splice(index, 1);
				return true;
			}
			return false;
		}

		/**
		 * 检测如果渲染对象新增的子级是否需要添加到显示列表
		 * @param render
		 *
		 */
		public function checkAddRender(render : RenderData) : void
		{
			var index : int = -1;

			if(render.render.parent)
			{
				index = container.getChildIndex(render.render);
			}

			if(index == -1)
				return;
			var len : int = render.numChildren;
			var child : RenderData;
			var i : int

			for(i = 0; i < len; i += 1)
			{
				child = render.getChildAt(i);

				if(child != null)
					index = addToContainer(child, index);
			}
		}

		/**
		 * 更新渲染列表.按照x,y深度排序
		 * @param elapsedTime
		 *
		 */
		override public function update(elapsedTime : uint) : void
		{
			updateChildIndexs();
		}

		protected function updateChildIndexs() : void
		{
			renderList.sortOn(["y" , "zIndex","id"] , [Array.NUMERIC , Array.NUMERIC,Array.DESCENDING | Array.NUMERIC]);

			var render : RenderData;
			var i : int = 0;

			for each(render in renderList)
			{
				i = updateRenderIndex(render, i);
			}
		}

		private function updateRenderIndex(render : RenderData, index : int) : int
		{
			if(render.render && render.render.parent)
			{
				if(index >= container.numChildren) //防止溢出
					index = container.numChildren - 1;
				render.render.parent.setChildIndex(render.render, index++);
			}

			var len : int = render.numChildren;
			var child : RenderData;
			var i : int;

			for(i = 0; i < len; i += 1)
			{
				child = render.getChildAt(i);

				if(child)
					index = updateRenderIndex(child, index);
			}
			return index;
		}

		/**
		 * 添加渲染对象,和它的子类对象,到场景中
		 * @param render
		 * @param index
		 * @return
		 *
		 */
		private function addToContainer(render : RenderData, index : int) : int
		{
			if(render.render) //如果指定为不需要添加到渲染列表
			{
				if(index > container.numChildren) //防止超出范围
					index = container.numChildren;
				container.addChildAt(render.render, index++);
			}

			var len : int = render.numChildren;
			var child : RenderData;
			var i : int;

			for(i = 0; i < len; i++)
			{
				child = render.getChildAt(i);

				if(child != null)
					index = addToContainer(child, index);
			}
			return index;
		}

		public function removeFromContainer(render : RenderData) : void
		{
			if(render.render && render.render.parent != null)
			{
				render.render.parent.removeChild(render.render);
			}

			var len : int = render.numChildren;
			var child : RenderData;
			var i : int;

			for(i = len - 1; i >= 0; i--)
			{
				child = render.getChildAt(i);

				if(child != null)
					removeFromContainer(child);
			}
		}

		/**
		 *
		 * @param level   更新的等级,0低级,1中级,2高级
		 * @param priority优先级别
		 *
		 */
		override public function registerd(registerdLevel : int = LOW, priority : int = 0) : void
		{
			if(GameManager.getInstance().addUpdate(this, registerdLevel, priority))
			{
				this.registerdLevel = registerdLevel;
			}
		}

		override public function unRegisterd() : void
		{
			GameManager.getInstance().removeUpdate(this, registerdLevel);
		}

		/**
		 * 销毁
		 *
		 */
		override public function destroy() : void
		{
			while(container.numChildren > 0)
			{
				container.removeChildAt(0);
			}
			renderList.length = 0;
			super.destroy();
		}
	}
}